load c:\manoj\projects\old_projects\plasma\HUA_daily.mat
HUA_daily = HUA_daily-repmat(nanmean(HUA_daily(:,500:600),2),[1,1440]); %remove midnight ? mean
load c:\manoj\projects\old_projects\plasma\HUA_PIU1 magnetic_frac;
load c:\manoj\projects\ace\substorm;
% load c:\manoj\projects\substorm\selected_substorm_quiet selected_substorm;
% fday_substorm = selected_substorm;

%load c:\manoj\projects\ace\rand_substorm;
hua_mean = nanmean(HUA_daily);
%day
st = 14/24;
en = 20/24;
%night
% st = 3/24;
% en = 7/24;

i_counter=0;
radius_epoch = 1/24;

for i = 1:length(fday_substorm),
    
    
    if fday_substorm(i)-floor(fday_substorm(i)) > st & ...
            fday_substorm(i)-floor(fday_substorm(i)) < en,
        
       [fday_int,ii] = intersect(HUA_fday,floor(fday_substorm(i)));
       
       if ~isempty(fday_int),
          fday_frac = HUA_fday(ii)+magnetic_frac;
           if fday_frac(1) < fday_substorm(i)-radius_epoch & ...
               fday_frac(end) > fday_substorm(i)+radius_epoch,
                     i_counter = i_counter+1;
           
           L = fday_frac >= fday_substorm(i)-radius_epoch & ...
               fday_frac <= fday_substorm(i)+radius_epoch;
           
           if sum(L) > floor(radius_epoch*2*1440),
               kkk = find(L ==1);
               L(kkk(1)) = 0;
           end;
           dummy= HUA_daily(ii,:)-hua_mean; %remove the daily (Sq +EEJ - sort of)
           SEG_HUA(i_counter,:) = dummy(L);
           end;
           
           end;
       end;
    end;


plot(-radius_epoch*1440+0.5:radius_epoch*1440-0.5,nanmean(SEG_HUA),'r');